package cn.buk.tms.flight.service;

import cn.buk.api.szkj.dto.eterm.PnrInfoPassenger;
import cn.buk.api.szkj.dto.eterm.response.RtpnrResponse;
import cn.buk.tms.entity.order.flight.FlightOrder;
import cn.buk.tms.entity.order.flight.FlightOrderPassenger;
import cn.buk.qms.service.BaseFlightOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 政府采购票的自动出票处理
 * @author yfdai
 */
@Service
public class AutoTicketGpWorker extends BaseFlightOrderService implements AutoTicketStrategy {

  private final EtermService etermService;

  @Autowired
  public AutoTicketGpWorker(EtermService etermService) {
    this.etermService = etermService;
  }

  /**
   * GP开票：建控PNR，开票完成，则提取票号回填
   * @param enterpriseId 当前用户的企业id
   * @param username 当前用户的用户名
   * @param orderId 机票订单id
   */
  @Override
  public void autoTicketFlightOrder(int enterpriseId, String username, int orderId) {
    FlightOrder orderInfo = flightOrderDao.getOrderDetailById(orderId);

    RtpnrResponse response = etermService.rt(enterpriseId, username, orderInfo.getPnrNo());
    if (response.getCode() == 1 && "1".equalsIgnoreCase(response.getPnrInfo().getIsEt())) {
      //已开票，读取票号，回填
      //直接用返回的结果回填票号和佣金
      //DONE 回填票号
      for (PnrInfoPassenger psgInfo0: response.getPnrInfo().getPassengers()) {
        for(FlightOrderPassenger psgInfo1: orderInfo.getPassengers()) {
          if (psgInfo1.getName().equalsIgnoreCase(psgInfo0.getName())) {
            psgInfo1.setTicketNo(psgInfo0.getTicketNo());
          }
        }
      }
      execUpdateTicketNo(enterpriseId, username, orderInfo, 1);
    }
  }
}
